/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var searchRange = function (nums, target) {
  if (nums.length === 0) {
    return [-1, -1]
  }
  let left = 0
  let right = nums.length - 1
  let range = []
  const getRange = (dir, index) => {
    if (dir === 'left') {
      let indexRange = index
      while (nums[indexRange] === target) {
        indexRange++
      }
      range.push(--indexRange)
    } else {
      let indexRange = index
      while (nums[indexRange] === target) {
        indexRange--
      }
      range.unshift(++indexRange)
    }
  }
  while (left <= right) {
    if (nums[left] === target) {
      range.push(left)
      getRange('left', left)
      return range
    } else if (nums[right] === target) {
      range.push(right)
      getRange('right', right)
      return range
    } else {
      left++
      right--
    }
  }
  return [-1, -1]
}
console.log(searchRange([1], 1))
